package com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.service.TaskFromService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class VillageHouseCheck implements IExcelVerifyHandler {
    /**
     * 错误信息
     */
    private StringBuilder msg;

    /**
     * 省市区地区map
     */
    private HashMap<String, List> map;

    @Autowired
    private TaskFromService taskFromService;

    public  static VillageHouseCheck villageHouseCheck;

    @PostConstruct
    public void init(){
        villageHouseCheck=this;
        villageHouseCheck.taskFromService=this.taskFromService;
    }

    public static Map<String, Object> getUserInfo(String userCode){
        return villageHouseCheck.taskFromService.getUserInfo(userCode);
    }

    public VillageHouseCheck() {
    }

    public VillageHouseCheck(HashMap map) {
        this.map = map;
    }

    @Override
    public ExcelVerifyHandlerResult verifyHandler(Object o) {
        // 默认为true
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        msg = new StringBuilder();
        boolean hasMsg = isHasMsg(o);
        if (hasMsg) {
            result.setSuccess(false);
            result.setMsg(msg.toString());
        }
        return result;
    }

    /**
     * 是否有错误消息
     */
    private boolean isHasMsg(Object o) {
        if (isFieldHasMsg(o)) {
            msg.append("填写错误");
            return true;
        }
        return false;
    }

    /**
     * 校验字段是否市下拉列表中的字典值
     */
    private boolean isFieldHasMsg(Object o) {
        boolean flag = false;

        // 所属省份
        String province = getFieldValueByFieldName("belongtoProvince", o);
        if (province != null && !map.containsKey(province)) {
            msg.append("所属省份(填写全名称,检查省市县关系),");
            flag = true;
        } else {
            String city = getFieldValueByFieldName("belongtoCity", o);
            // 所属地级市
            if (!"北京市".equals(province) && !"天津市".equals(province) && !"上海市".equals(province) && !"重庆市".equals(province)) {
                List<String> cities = map.get(province);
                if (city != null && cities != null && !cities.contains(city)) {
                    msg.append("所属地级市(填写全名称,省市关系不匹配),");
                    flag = true;
                } else {
                    // 所属区县
                    String area = getFieldValueByFieldName("belongtoArea", o);
                    List<String> areas = map.get(city);
                    if (area != null && areas != null && !areas.contains(area)) {
                        msg.append("所属区县(填写全名称,市县关系不匹配),");
                        flag = true;
                    }
                }
            }
            // 所属地级市为直辖市
            else {
                if (city != null && !province.equals(city)) {
                    msg.append("所属地级市(填写全名称,直辖市应与省份同名)");
                    flag = true;
                } else {
                    // 所属区县
                    String area = getFieldValueByFieldName("area", o);
                    List<String> areas = map.get(city);
                    if (area != null && areas != null && !areas.contains(area)) {
                        msg.append("所属区县(填写全名称,市县关系不匹配)");
                        flag = true;
                    }
                }
            }
        }

        // 被调查建筑分类(下拉选项)
        String bulidingTypeField = getFieldValueByFieldName("bulidingType", o);
        Map bulidingType=new HashMap<>();
        bulidingType.put("既有","");
        bulidingType.put("新增","");
        bulidingType.put("已拆除","");
        boolean bulidingTypeBoo = bulidingType.containsKey(bulidingTypeField);
        if (bulidingTypeField != null && !bulidingTypeBoo) {
            msg.append("被调查建筑分类(下拉选项),");
            flag = true;
        }

        // 结构类型(下拉选项)
        String structureTypeField = getFieldValueByFieldName("structureType", o);
        Map structureType=new HashMap<>();
        structureType.put("砖(砌块)混","");
        structureType.put("低层框架","");
        structureType.put("轻钢(钢)装配式","");
        structureType.put("混凝土装配式","");
        structureType.put("砖木","");
        structureType.put("土木","");
        structureType.put("石木","");
        structureType.put("木结构","");
        structureType.put("窑洞","");
        structureType.put("混杂结构(砖/砌块/土/石)","");
        structureType.put("其它","");
        boolean structureTypeBoo = structureType.containsKey(structureTypeField);
        if (structureTypeField != null && !structureTypeBoo) {
            msg.append("结构类型(下拉选项),");
            flag = true;
        }

        // 建造方式(下拉选项)
        String buildMethodField = getFieldValueByFieldName("buildMethod", o);
        Map buildMethod=new HashMap<>();
        buildMethod.put("自建","");
        buildMethod.put("统建","");
        buildMethod.put("其它","");
        boolean buildMethodBoo = buildMethod.containsKey(buildMethodField);
        if (buildMethodField != null && !buildMethodBoo) {
            msg.append("建造方式(下拉选项),");
            flag = true;
        }

        // 建造时间(下拉选项)
        String buildingTimeField = getFieldValueByFieldName("buildingTime", o);
        Map buildingTime=new HashMap<>();
        buildingTime.put("70s以前","");
        buildingTime.put("70s","");
        buildingTime.put("80s","");
        buildingTime.put("90s","");
        buildingTime.put("00s","");
        buildingTime.put("10s","");
        boolean buildingTimeBoo = buildingTime.containsKey(buildingTimeField);
        if (buildingTimeField != null && !buildingTimeBoo) {
            msg.append("建造时间(下拉选项),");
            flag = true;
        }

        // 使用情况(下拉选项)
        String houseUseSituationField = getFieldValueByFieldName("houseUseSituation", o);
        Map houseUseSituation=new HashMap<>();
        houseUseSituation.put("自用","");
        houseUseSituation.put("空置","");
        houseUseSituation.put("出租","");
        houseUseSituation.put("经营","");
        houseUseSituation.put("被征或待征","");
        houseUseSituation.put("其它","");
        boolean houseUseSituationBoo = houseUseSituation.containsKey(houseUseSituationField);
        if (houseUseSituationField != null && !houseUseSituationBoo) {
            msg.append("使用情况(下拉选项),");
            flag = true;
        }

        // 抗震设防加固情况(下拉选项)
        String houseReinforceSituationField = getFieldValueByFieldName("houseReinforceSituation", o);
        Map houseReinforceSituation=new HashMap<>();
        houseReinforceSituation.put("未设防","");
        houseReinforceSituation.put("6度(0.05g)","");
        houseReinforceSituation.put("7度(0.10g&0.15g)","");
        houseReinforceSituation.put("8度(0.20g&0.30g)","");
        houseReinforceSituation.put("9度(0.40g)","");
        boolean houseReinforceSituationBoo = houseReinforceSituation.containsKey(houseReinforceSituationField);
        if (houseReinforceSituationField != null && !houseReinforceSituationBoo) {
            msg.append("抗震设防加固情况(下拉选项),");
            flag = true;
        }

        // 抗震构造措施(下拉选项)
        String houseMeasuresField = getFieldValueByFieldName("houseMeasures", o);
        Map houseMeasures=new HashMap<>();
        houseMeasures.put("基本完备","");
        houseMeasures.put("部分具备","");
        houseMeasures.put("完全没有","");
        boolean houseMeasuresBoo = houseMeasures.containsKey(houseMeasuresField);
        if (houseMeasuresField != null && !houseMeasuresBoo) {
            msg.append("抗震构造措施(下拉选项),");
            flag = true;
        }

        // 抗震构造措施情况(下拉选项)
        String houseMeasuresSituationField = getFieldValueByFieldName("houseMeasuresSituation", o);
        Map houseMeasuresSituation=new HashMap<>();
        houseMeasuresSituation.put("正规设计建造","");
        houseMeasuresSituation.put("有圈梁构造柱等整体性连接措施","");
        houseMeasuresSituation.put("已实施抗震加固","");
        houseMeasuresSituation.put("其它抗震措施","");
        boolean houseMeasuresSituationBoo = houseMeasuresSituation.containsKey(houseMeasuresSituationField);
        if (houseMeasuresSituationField != null && !houseMeasuresSituationBoo) {
            msg.append("抗震构造措施情况(下拉选项),");
            flag = true;
        }

        // 有无明显可见的裂缝、变形、倾斜等(下拉选项)
        String buildingIsDamageField = getFieldValueByFieldName("buildingIsDamage", o);
        Map buildingIsDamage=new HashMap<>();
        buildingIsDamage.put("有","");
        buildingIsDamage.put("无","");
        buildingIsDamage.put("不确定","");
        boolean buildingIsDamageBoo = buildingIsDamage.containsKey(buildingIsDamageField);
        if (buildingIsDamageField != null && !buildingIsDamageBoo) {
            msg.append("有无明显可见的裂缝、变形、倾斜等(下拉选项),");
            flag = true;
        }

        // 受灾情况(下拉选项)
        String houseDisasterSituationField = getFieldValueByFieldName("houseDisasterSituation", o);
        Map houseDisasterSituation=new HashMap<>();
        houseDisasterSituation.put("未受灾","");
        houseDisasterSituation.put("曾受灾","");
        boolean houseDisasterSituationBoo = houseDisasterSituation.containsKey(houseDisasterSituationField);
        if (houseDisasterSituationField != null && !houseDisasterSituationBoo) {
            msg.append("受灾情况(下拉选项),");
            flag = true;
        }

        // 是否有辅助用房(下拉选项)
        String isAuxiliaryHouseField = getFieldValueByFieldName("isAuxiliaryHouse", o);
        Map isAuxiliaryHouse=new HashMap<>();
        isAuxiliaryHouse.put("有","");
        isAuxiliaryHouse.put("无","");
        boolean isAuxiliaryHouseBoo = isAuxiliaryHouse.containsKey(isAuxiliaryHouseField);
        if (isAuxiliaryHouseField != null && !isAuxiliaryHouseBoo) {
            msg.append("是否有辅助用房(下拉选项),");
            flag = true;
        }

        //初步结论(下拉选项)
        String resultField = getFieldValueByFieldName("result", o);
        Map result=new HashMap<>();
        result.put("满足抗震设防要求且质量较好","");
        result.put("不满足抗震设防要求且质量很差","");
        result.put("需要进一步鉴定","");
        boolean resultBoo = result.containsKey(resultField);
        if (resultField != null && !resultBoo) {
            msg.append("初步结论(下拉选项),");
            flag = true;
        }

        //采集员账号
        String userCode = getFieldValueByFieldName("userCode", o);
        Map<String, Object> userInfo = getUserInfo(userCode);
        if (userCode!=null && userInfo.get("userId") == null ) {
            msg.append("采集员账号,");
            flag = true;
        }
        return flag;
    }





    /**
     * 根据属性名获取属性值
     *
     * @param fieldName 属性名
     * @param object    对象
     * @return
     */
    private String getFieldValueByFieldName(String fieldName, Object object) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = object.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(object, new Object[]{});
            return value.toString();
        } catch (Exception e) {
            return null;
        }
    }
}
